<?php

namespace App\Http\Middleware;

use Closure;
use InfyOm\Generator\Utils\ResponseUtil;
use Response;
class InnerApi
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $res =  \GuzzleHttp\json_decode($response->getContent());

        $message=$res->message;
        $data=isset($res->data) ? $res->data : [];

        if($res->status==0){
            return Response::json(ResponseUtil::makeError($message,$data));
        }

        return Response::json(ResponseUtil::makeResponse($message,$data));
    }
}
